package com.ruoyi.web.controller.dataStatistics;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.RolesEnums;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.construction.domain.ConstructionSite;
import com.ruoyi.modules.domain.ConstructionWasteTransportationOrder;
import com.ruoyi.modules.domain.TransportationCompany;
import com.ruoyi.modules.service.IConstructionWasteTransportationOrderService;
import com.ruoyi.modules.service.ITransportationCompanyService;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.wasterTran.dto.OrderDto;
import com.ruoyi.wasterTran.vo.BillStatisticsVo;
import com.ruoyi.wasterTran.vo.WasterTypeDataVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 数据统计
 */
@RestController
@RequestMapping("/data/static")
public class DataStatisticsController extends BaseController {
    @Autowired
    private ITransportationCompanyService companyService;
    @Autowired
    private ISysDictDataService sysDictDataService;
    @Autowired
    private IConstructionWasteTransportationOrderService orderService;
    /**
     * 查询车辆总数 统计
     * @param company
     * @return
     */
    @GetMapping("/selectTranCompanyCount")
    public AjaxResult  selectTranCompanyCount(@RequestBody TransportationCompany company){

        SysUser user = getLoginUser().getUser();
        Long roleId = user.getRoleId();
        String roleKey = RolesEnums.getRoleKeyByRoleId(roleId);
        // 运输公司管理员
        if(RolesEnums.YUN_SHU_ADMIN.getRoleKey().equals(roleKey)){
            company.setUserId(getLoginUser().getUser().getUserId()+"");
        }
        return AjaxResult.success(companyService.selectTransportationCompanyList(company));
    }

    /**
     * 查询地区，按照年，月，日统计运输次数，运输量订单统计
     * @param orderDto  type传值为1 表示按月统计
     * @return
     */
    @PostMapping("/selectOrderCountByTimeType")
    public AjaxResult selectOrderCountByTimeType(@RequestBody OrderDto orderDto){
        List<BillStatisticsVo> result = new ArrayList<>();
        String year = DateUtils.year();
        if(orderDto.getType().equals("1")){
            for (int i = 1; i <= 12; i++) {
                BillStatisticsVo bv =new BillStatisticsVo();
                bv.setCount("0");
                bv.setQuality("0");
                if(i<10){
                    bv.setTime(year+"-0"+i);
                }else{
                    bv.setTime(year+"-"+i);
                }
                result.add(bv);
            }
        }
        System.out.println(result);
        List<BillStatisticsVo> list =  orderService.selectOrderCount(orderDto);
        System.out.println(list);
        if(list.size()>0){
            Map<String, BillStatisticsVo> collect = list.stream().collect(Collectors.toMap(BillStatisticsVo::getTime, v -> v));
            System.out.println(collect);
            result.forEach(item->{
                if(collect.containsKey(item.getTime()+"")){
                    item.setCount(collect.get(item.getTime()+"").getCount());
                }
                if(collect.containsKey(item.getTime()+"")){
                    item.setQuality(collect.get(item.getTime()+"").getQuality());
                }

            });
        }
        return AjaxResult.success(result);
    }

    /**
     * TODO 根据垃圾类型统计运输量，运输次数
     * @return
     */
    @PostMapping("/selectOrderWasterTypeCount")
    public AjaxResult selectOrderWasterTypeCount(@RequestBody OrderDto orderDto){
        SysDictData sysDictData = new SysDictData();
        sysDictData.setDictType("rubbish_type");
        List<SysDictData> dictDataList = sysDictDataService.selectDictDataList(sysDictData);
        List<WasterTypeDataVo> result = new ArrayList<>();
        dictDataList.forEach(dictData->{
            WasterTypeDataVo wasterTypeDataVo = new WasterTypeDataVo();
            wasterTypeDataVo.setWasterType(dictData.getDictValue());
            wasterTypeDataVo.setWasterTypeName(dictData.getDictLabel());
            wasterTypeDataVo.setTransCount("0");
            wasterTypeDataVo.setTotalMassCount("0");
            result.add(wasterTypeDataVo);
        });
        List<WasterTypeDataVo> wasterTypeDataVos =  orderService.selectOrderWasterTypeCount(orderDto);
        if(wasterTypeDataVos!=null){
            Map<String, WasterTypeDataVo> collect = wasterTypeDataVos.stream().collect(Collectors.toMap(WasterTypeDataVo::getWasterType, v -> v));
            result.forEach(item->{
               if(collect.containsKey(item.getWasterType())){
                    item.setTransCount(collect.get(item.getWasterType()).getTransCount());
                    item.setTotalMassCount(collect.get(item.getWasterType()).getTotalMassCount());
               }
            });
        }
        return AjaxResult.success(result);
    }

    /**
     * TODO 统计每个工地的垃圾运出量，运输次数
     * @return
     */
    @GetMapping("/selectOrderBySiteCount")
    public TableDataInfo selectOrderBySiteCount(){
        return null;
    }
    /**
     * TODO 统计每个处置场所的垃圾接收量，运输次数
     * @return
     */
    @GetMapping("/selectOrderByDispatalSiteCount")
    public AjaxResult selectOrderByDispatalSiteCount(){
        return null;
    }


    /**
     *  联单总数,运输总量, 运输次数 ,处置总量
     * @return
     */
    @PostMapping("/countNum")
    public AjaxResult countNum(@RequestBody OrderDto orderDto){
        SysUser user = getLoginUser().getUser();
        Long roleId = user.getRoleId();
        String roleKey = RolesEnums.getRoleKeyByRoleId(roleId);
        String  userId = null;
        // 运输公司管理员
        if(RolesEnums.YUN_SHU_ADMIN.getRoleKey().equals(roleKey)){
            userId = user.getTransportationCompanyId();
            orderDto.setUserId(userId);
        }
        List<Map<String, Object>>  lsit = orderService.countNum(orderDto);
        return AjaxResult.success(lsit);
    }


    /**
     *  状态分布
     * @return
     */
    @PostMapping("/selectOrderByStatus")
    public AjaxResult selectOrderByStatus(@RequestBody OrderDto orderDto){
        SysUser user = getLoginUser().getUser();
        Long roleId = user.getRoleId();
        String roleKey = RolesEnums.getRoleKeyByRoleId(roleId);
        // 运输公司id
        String  userId = null;
        // 运输公司管理员
        if(RolesEnums.YUN_SHU_ADMIN.getRoleKey().equals(roleKey)){
            userId = user.getTransportationCompanyId();
            orderDto.setUserId(userId);
        }
        List<Map<String, Object>>  lsit = orderService.selectOrderByStatus(orderDto);
        return AjaxResult.success(lsit);
    }




    /**
     *  工地统计列表
     * @return
     */
    @GetMapping("/selectOrderList")
    public TableDataInfo selectOrderList(OrderDto orderDto){
        SysUser user = getLoginUser().getUser();
        Long roleId = user.getRoleId();
        String roleKey = RolesEnums.getRoleKeyByRoleId(roleId);
        String  userId = null;
        // 运输公司管理员
        if(RolesEnums.YUN_SHU_ADMIN.getRoleKey().equals(roleKey)){
            userId = user.getTransportationCompanyId();
            orderDto.setUserId(userId);
        }
        startPage();
        List<Map<String, Object>> list = orderService.selectOrderList(orderDto);
        return getDataTable(list);
    }

    /**
     *  工地区域分布统计
     * @return
     */
    @PostMapping("/selectConstructionSiteArea")
    public AjaxResult  selectConstructionSiteArea(@RequestBody OrderDto orderDto){
        SysUser user = getLoginUser().getUser();
        Long roleId = user.getRoleId();
        String roleKey = RolesEnums.getRoleKeyByRoleId(roleId);
        String  userId = null;
        // 运输公司管理员
        if(RolesEnums.YUN_SHU_ADMIN.getRoleKey().equals(roleKey)){
            userId = user.getTransportationCompanyId();
            orderDto.setUserId(userId);
        }
        List<Map<String, Object>>  lsit = orderService.selectConstructionSiteArea(orderDto);
        return AjaxResult.success(lsit);
    }

}
